import { ButtonTab, ButtonTabItem, Popover } from "vux";
export default {
  name: "Cart",
  components: {
    ButtonTab,
    ButtonTabItem,
    Popover
  },
  data() {
    return {
      searchIcon: "\ue630 搜索店铺",
      dataArr: [],
      loading: true,
      listHeight: "",
      marginTopHeight: "",
      keyWord: "",
      btnState: 0
    };
  },
  methods: {
    goHomePage() {
      this.$callJava({ code: 1007 });
    },
    goBack() {
      // this.$callJava({code:1007})
      this.$router.go(-1);
      // if (this.$fromApp(this)) {
      //   if (this.$route.query.fromPush) {
      //     this.$callJava({ code: 1007 });
      //   }
      // }
    },
    goShop(val) {
      //获取经销商信息
      // if(this.$fromApp(this)){
      //   this.$ajax.get('common/distributor/info', res => {
      //     if (res.code == '200' && res.data) {
      //       if (res.data.erpStatus == 1) {
      //         this.$callJava({url:this.$webUrl+"index?from=app&id="+val.id + '&zeroSaleState=' + val.zeroSaleFlag+'&fromManage=1'})
      //       } else {
      //         this.$callJava({url:this.$webUrl+"purchase/storePage?from=app&id="+val.id + '&zeroSaleState=' + val.zeroSaleFlag+'&fromManage=1'})
      //       }
      //     }
      //   }, {distributorId: val.id});
      //   // this.$callJava({
      //   //   old: {url:this.$webUrl+"purchase/storePage?from=app&id="+val.id + '&zeroSaleState=' + val.zeroSaleFlag+'&fromManage=1'},
      //   //   new: {url:this.$webUrl+"index?from=app&id="+val.id + '&zeroSaleState=' + val.zeroSaleFlag+'&fromManage=1'}
      //   // })
      // }else{
      this.$ajax.get(
        "common/distributor/info",
        res => {
          if (res.code == "200" && res.data) {
            if (res.data.erpStatus == 1) {
              this.$router.push({
                path: "/index",
                query: {
                  id: val.id,
                  zeroSaleState: val.zeroSaleFlag,
                  fromManage: 1,
                  from: this.$fromApp(this) ? "app" : ""
                }
              });
            } else {
              this.$router.push({
                path: "/purchase/storePage",
                query: {
                  id: val.id,
                  zeroSaleState: val.zeroSaleFlag,
                  fromManage: 1,
                  from: this.$fromApp(this) ? "app" : ""
                }
              });
            }
          }
        },
        { distributorId: val.id }
      );

      // }
    },
    getData(isInput = 0) {
      var type;
      if (isInput == 0) {
        if (0 == this.btnState) {
          type = 1;
        } else {
          type = "";
        }
      } else {
        type = "";
        if (this.keyWord == "") {
          if (0 == this.btnState) {
            type = 1;
          } else {
            type = "";
          }
        }
      }

      this.$ajax.get(
        "replenish/shop/list",
        res => {
          if (res.code == 200) {
            this.dataArr = res.data;
          } else {
            this.$vux.toast.text(res.msg, "center");
          }
        },
        { focusState: type, keywords: this.keyWord }
      );
      //focusState: 1、关注 0、不关注  空、全部
    },
    setTop(state, item, index) {
      //置顶
      // if(1 == state){
      //   item.isTop = 0;
      //   this.$set(this.dataArr,index,item);
      //   this.dataArr.splice(index,1);
      //   this.dataArr.splice(0,0,item);
      // }else{
      //   item.isTop = 1;
      //   this.$set(this.dataArr,index,item);
      // }
      this.$ajax.post(
        "replenish/shop/update/isTop",
        { distributorId: item.id, isTop: state },
        res => {
          if (res.code == 200) {
            // console.log('置顶返回结果：',res);
            if (state == 1) {
              this.$vux.toast.text("置顶成功", "center");
            } else {
              this.$vux.toast.text("取消置顶成功", "center");
            }
            this.getData();
          } else {
            this.$vux.toast.text(res.msg, "center");
          }
        }
      );
    },
    setFocus(state, item, index) {
      //关注
      this.$ajax.post(
        "replenish/shop/update/focusState",
        { distributorId: item.id, focusState: state },
        res => {
          if (res.code == 200) {
            // console.log('关注返回结果：',res);
            if (state == 1) {
              this.$vux.toast.text("关注成功", "center");
            } else {
              this.$vux.toast.text("取消关注成功", "center");
            }
            this.getData();
          } else {
            this.$vux.toast.text(res.msg, "center");
          }
        }
      );
    }
  },
  mounted() {
    console.log(this.$route)
    sessionStorage.clear();
    this.loading = true;
    this.listHeight = (document.body.clientHeight * 2) / 100 - 2 + "rem";
    if (this.$fromApp(this)) {
      this.marginTopHeight = "0";
    } else {
      this.marginTopHeight = "0.92rem";
    }

    // let id = this.$route.query.sellId;

    this.getData();
    // this.$callJava({ onBackPressed: true });
    window["onBackPressed"] = this.goHomePage.bind(this);
  },
  beforeDestroy(){
    // this.$callJava({ onBackPressed: false });
  }
};
